﻿using NetBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetBase.Net
{
    public class Rel<T1, T2> : IRel 
        where T1 : BaseElem 
        where T2 : BaseElem
    {
        public T1 Me { get { return el1; } }
        public T2 Other { get { return el2; } }
        public ConTag MeTag { get { return tag1; } }
        public ConTag OtherTag { get { return tag2; } }
        public Rel<T2, T1> OtherRel { get { return rel2; } }


        protected T1 el1;
        protected ConTag tag1;
        protected T2 el2;        
        protected ConTag tag2;
        protected Rel<T2, T1> rel2;

        public Rel(T1 el1, ConTag tag1, T2 el2, ConTag tag2) : this (el1, tag1, el2, tag2, null)
        {
            rel2 = new Rel<T2, T1>(el2, tag2, el1, tag1, this);
        }

        protected Rel(T1 el1, ConTag tag1, T2 el2, ConTag tag2, Rel<T2,T1> rel2)
        {
            this.el1 = el1;
            this.tag1 = tag1;
            this.el2 = el2;
            this.tag2 = tag2;
            this.rel2 = rel2;
            // Add links to elements
            el1.Rels.Add(this);
            el2.Rels.Add((IRel)(this.OtherRel));
        }

        object IRel.Me
        {
            get { return el1 as Object; }
        }

        object IRel.Other
        {
            get { return el2 as Object; }
        }

        IRel IRel.OtherRel
        {
            get { return rel2; }
        }
    }
}
